﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Logon : System.Web.UI.Page
{
    static int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            i = 0;
        }
    }

    protected void LinkZc_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }

    protected void cz_Click(object sender, EventArgs e)
    {
        this.TxtUser.Text = null;
        this.TxtPwd.Text = null;
    }

    protected void dl_Click(object sender, EventArgs e)
    {
        string sql = string.Format("select * from Doctor where ID='{0}'", this.TxtUser.Text);
        SqlDataReader reader = DBHelper.GetDataReader(sql);
        string pwd = "";
        string IdyID = "";
        while (reader.Read())
        {
            pwd = reader["PassWord"].ToString();
            IdyID = reader["IdentityID"].ToString();
            
        }
        reader.Close();
        if (this.TxtUser.Text != "" && pwd !="")
        {
            if (this.TxtPwd.Text == pwd)
            {
                if (IdyID == "1")
                {
                    Session["IdyID"] = IdyID;
                    Session["Name"] = this.TxtUser.Text;
                    Response.Redirect("AdsIndex.aspx");
                }
                if (IdyID == "2")
                {
                    Session["IdyID"] = IdyID;
                    Session["Name"] = this.TxtUser.Text;
                    Response.Redirect("DtrIndex.aspx");
                }
            }
            else
            {
                i = i + 1;
                Response.Write("<script> alert('登录失败，账号或密码错误。')</script>");
            }
            if (i >= 3)
            {
                Response.Write("<script> alert('登录失败已达上限，请联系管理员。')</script>");
                this.dl.Enabled = false;
            }
        }
        else
        {
            Response.Write("<script> alert('请输入正确的账号。')</script>");
        }
        
    }

    protected void Btnhzdl_Click(object sender, EventArgs e)
    {
        string sql1 = string.Format("select * from Patient where IDCard='{0}'", this.TxtUser.Text);
        SqlDataReader re = DBHelper.GetDataReader(sql1);
        string pwd = "";
        string IdyID = "";
        while (re.Read())
        {
            pwd = re["PassWord"].ToString();
            IdyID = re["IdentityID"].ToString();
        }
        re.Close();
        if (this.TxtUser.Text != "" && pwd != "")
        {
            if (this.TxtPwd.Text == pwd)
            {
                Session["IdyID"] = IdyID;
                Session["Name"] = TxtUser.Text;
                Response.Redirect("PatIndex.aspx");
            }
            else
            {
                Response.Write("<script> alert('登录失败，账号或密码错误。')</script>");
            }
        }
        else
        {
            Response.Write("<script> alert('请输入正确的账号。')</script>");
        }
    }


    protected void Linkwjmm_Click(object sender, EventArgs e)
    {
        Response.Redirect("PassWord.aspx");
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("DocPassWord.aspx");
    }

}